<?php
class XDao_FinderI18n extends XDao_Finder
{
    // language
    protected $langId;

	/**
	 * Stores table schema.
	 *
	 * @return void
	 */
	public function __construct($schema, $langId)
	{
		parent::__construct($schema);
		$this->langId = $langId;
	}

	/**
	 * Build the SQL query based on all set conditions.
	 *
	 * @param   boolean  indicates whether to build a count query
	 * @param   boolean  indicates whether the query SELECTS or DELETES
	 * @return  string
	 */
	public function build_query($count_rows = FALSE, $delete =  FALSE)
	{
		if (isset($this->schema->translated_cols) && !empty($this->schema->translated_cols))
		{
			$this->sql_conditions['select'][] = $this->schema->table . '.*';
			foreach ($this->schema->translated_cols as $key => $col_name)
			{
				$this->sql_conditions['select'][] = 'translations_' . $this->langId . '_' . $col_name . '.trans_content AS ' . $col_name;
				$this->sql_conditions['external_join'][] = array
				(
					XDb::JOIN_TABLE => 'translations AS translations_' . $this->langId . '_' . $col_name, 
					XDb::JOIN_CONDITIONS => array
					(
						array($this->schema->table . '.' . $this->schema->columns[$this->schema->primary]->name, '=', 'translations_' . $this->langId . '_' . $col_name . '.object_id' , ''),
						array('translations_' . $this->langId . '_' . $col_name . '.lang_id', '=', $this->langId, 'AND'),
						array('translations_' . $this->langId . '_' . $col_name . '.trans_type', '=', '"' . $col_name . '"', 'AND')
					)
				);
			}
		}
		$sql = parent::build_query($count_rows = FALSE, $delete = FALSE);

		return $sql;
	}

	/**
	 * Instantiate an empty item for current table.
	 *
	 * @return  object
	 */
	public function fetch_new()
	{
		if (isset($this->schema->item))
		{
			$item = new $this->schema->item($this->schema, $this->langId);
		}
		else
		{
			$item = new XDao_ItemI18n($this->schema, $this->langId);
		}
		return $item;
	}	
	
}
?>